import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:plugin_native/controller/flutter_egis_map_controller.dart';
import 'package:plugin_native/data/flutter_map_options_entity.dart';

/// 应急地图Widget
class TianDiMapWidget extends StatefulWidget {
  ///地图加载完成的通知
  final ValueChanged<TianDiMapController> onMapLoadComplete;

  ///地图基本配置
  final MapOptions mapOptions;

  TianDiMapWidget({
    required this.mapOptions,
    required this.onMapLoadComplete,
  });

  @override
  _TianDiMapWidgetState createState() => _TianDiMapWidgetState();
}

class _TianDiMapWidgetState extends State<TianDiMapWidget> {
  /// 唯一标识符
  final String type = "mapView";

  @override
  Widget build(BuildContext context) {
    return AndroidView(
      viewType: type,
      onPlatformViewCreated: onPlatformViewCreated,
      creationParams: widget.mapOptions.toMap() as dynamic, //向视图传递参数
      creationParamsCodec: StandardMessageCodec(), //编解码器类型
    );
  }

  /// 创建事件
  void onPlatformViewCreated(int id) {
    ///改造思路,调用complete方法,此处创建EgisMapController对象,complete参数回调回去
    TianDiMapController egisMapController = TianDiMapController(id);
    egisMapController.setMethodChannelHandler((call) {
      return Future(() {
        switch (call.method) {
          case "mapCompleted":
            widget.onMapLoadComplete(egisMapController);
            break;
          case "onPolygonClick":
            if (egisMapController.onPolygonClick != null) {
              egisMapController.onPolygonClick!(call.arguments.toString());
            }
            break;
          case "onInfoWindowClick":
            if (egisMapController.onInfoWindowClick != null) {
              egisMapController.onInfoWindowClick!(call.arguments.toString());
            }
            break;
        }
      });
    });
  }

  @override
  void didChangeDependencies() {
    print('didChangeDependencies');
    super.didChangeDependencies();
  }

  @override
  void dispose() {
    print('implement dispose');
    super.dispose();
  }

  @override
  void didUpdateWidget(TianDiMapWidget emergencyMapWidget) {
    print('didUpdateWidget');
    super.didUpdateWidget(emergencyMapWidget);
  }

  @override
  void reassemble() {
    print('reassemble');
    super.reassemble();
  }
}
